package com.zaey.auto.utils;

import com.baidu.aip.ocr.AipOcr;
import org.json.JSONArray;
import org.json.JSONObject;

import java.util.HashMap;

/**
 * @author JohnSoybean
 * @create 2020-05-23 0:33
 * @description
 */
public class AipUtils {
    public static AipOcr getClient() {
        // 初始化一个AipOcr
        AipOcr client = new AipOcr(AipConfig.APP_ID, AipConfig.API_KEY, AipConfig.SECRET_KEY);

        // 可选：设置网络连接参数
        client.setConnectionTimeoutInMillis(2000);
        client.setSocketTimeoutInMillis(60000);

        // 可选：设置代理服务器地址, http和socket二选一，或者均不设置
//        client.setHttpProxy("proxy_host", proxy_port);  // 设置http代理
//        client.setSocketProxy("proxy_host", proxy_port);  // 设置socket代理

        // 可选：设置log4j日志输出格式，若不设置，则使用默认配置
        // 也可以直接通过jvm启动参数设置此环境变量
//        System.setProperty("aip.log4j.conf", "path/to/your/log4j.properties");
        return client;
    }

    public static JSONObject requestNormal(AipOcr client, String url) {
        // 传入可选参数调用接口
        HashMap<String, String> options = new HashMap<String, String>();
        options.put("language_type", "CHN_ENG");
        options.put("detect_direction", "false");
        options.put("detect_language", "false");
        options.put("probability", "false");

        // 参数为本地图片路径
        String image = url;
        JSONObject res = client.basicGeneral(image, options);
        System.out.println(res.toString(2));
        return res;
//        // 参数为本地图片二进制数组
//        byte[] file = readImageFile(image);
//        res = client.basicGeneral(file, options);
//        System.out.println(res.toString(2));
//
//
//        // 通用文字识别, 图片参数为远程url图片
//        JSONObject res = client.basicGeneralUrl(url, options);
//        System.out.println(res.toString(2));
    }

    public static int countToday(JSONObject res) {
        JSONArray array = res.getJSONArray("words_result");
        int count = 0;
        for (int i = 0; i < array.length(); i++) {
            JSONObject obj = array.getJSONObject(i);
            String words = obj.getString("words");
            if (words.equals("今天")) {
                count++;
            }
        }
        return count;
    }

    public static Integer isToday(JSONObject res, String[] keywords) {
        JSONArray array = null;
        try {
            array = res.getJSONArray("words_result");
        } catch (Exception e) {
            System.out.println("达到限额");
            return 0;
        }
        if (array == null) {
            return -1;
        }
        int count = 0;
        for (int i = 0; i < array.length(); i++) {
            JSONObject obj = array.getJSONObject(i);
            String words = obj.getString("words");
            for (int j = 0; j < keywords.length; j++) {
                if (words.contains(keywords[j])) {
                    count++;
                }
            }
        }
        if (count >= keywords.length) {
            return 1;
        }else{
            return -1;
        }
    }

    public static boolean isKeywordExist(JSONObject res, String keyword) {
        JSONArray array = res.getJSONArray("words_result");
        if (array == null) {
            return false;
        }
        int count = 0;
        for (int i = 0; i < array.length(); i++) {
            JSONObject obj = array.getJSONObject(i);
            String words = obj.getString("words");
            if (words.contains(keyword)) {
                count++;
            }
        }
        if (count > 0) {
            return true;
        }else{
            return false;
        }
    }

    public static void main(String[] args) {
        AipOcr client = getClient();
        JSONObject res = requestNormal(client, "G:\\var\\1.jpg");
        int count = countToday(res);
        boolean is = isKeywordExist(res, "不要");
        System.out.println(count);
        System.out.println(is);
    }

}
